<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Migrating from libunique to GApplication or GtkApplication: GTK+ 3 Reference Manual</title>
<meta name="generator" content="DocBook XSL Stylesheets Vsnapshot">
<link rel="home" href="index.html" title="GTK+ 3 Reference Manual">
<link rel="up" href="migrating.html" title="Part V. Migrating from Previous Versions of GTK+">
<link rel="prev" href="gtk-migrating-GtkStyleContext-bonus-points.html" title="Bonus points">
<link rel="next" href="ch28s02.html" title="Commands and Messages">
<meta name="generator" content="GTK-Doc V1.33.1 (XML mode)">
<link rel="stylesheet" href="style.css" type="text/css">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table class="navigation" id="top" width="100%" summary="Navigation header" cellpadding="2" cellspacing="5"><tr valign="middle">
<td width="100%" align="left" class="shortcuts"></td>
<td><a accesskey="h" href="index.html"><img src="home.png" width="16" height="16" border="0" alt="Home"></a></td>
<td><a accesskey="u" href="migrating.html"><img src="up.png" width="16" height="16" border="0" alt="Up"></a></td>
<td><a accesskey="p" href="gtk-migrating-GtkStyleContext-bonus-points.html"><img src="left.png" width="16" height="16" border="0" alt="Prev"></a></td>
<td><a accesskey="n" href="ch28s02.html"><img src="right.png" width="16" height="16" border="0" alt="Next"></a></td>
</tr></table>
<div class="chapter">
<div class="titlepage"><div><div><h2 class="title">
<a name="gtk-migrating-unique-GtkApplication"></a>Migrating from libunique to GApplication or GtkApplication</h2></div></div></div>
<div class="toc"><dl class="toc">
<dt><span class="section"><a href="gtk-migrating-unique-GtkApplication.html#id-1.6.5.5">Uniqueness</a></span></dt>
<dt><span class="section"><a href="ch28s02.html">Commands and Messages</a></span></dt>
</dl></div>
<p>
    libunique offers 'unique application' support as well as ways to
    communicate with a running application instance. This is implemented
    in various ways, either using D-Bus, or socket-based communication.
  </p>
<p>
    Starting with GLib 2.26, D-Bus support has been integrated into GIO
    in the form of GDBus, and <span class="type">GApplication</span> has been added to provide
    the same level of application support as libunique.
  </p>
<div class="example">
<a name="id-1.6.5.4"></a><p class="title"><b>Example 52. A unique application</b></p>
<div class="example-contents">
<p>Here is a simple application using libunique:
  </p>
<div class="informalexample">
  <table class="listing_frame" border="0" cellpadding="0" cellspacing="0">
    <tbody>
      <tr>
        <td class="listing_lines" align="right"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32</pre></td>
        <td class="listing_code"><pre class="programlisting"><span class="kt">int</span>
<span class="nf">main</span> <span class="p">(</span><span class="kt">int</span> <span class="n">argc</span><span class="p">,</span> <span class="kt">char</span> <span class="o">*</span><span class="n">argv</span><span class="p">[])</span>
<span class="p">{</span>
  <span class="n">UniqueApp</span> <span class="o">*</span><span class="n">app</span><span class="p">;</span>
  <span class="n">GtkWidget</span> <span class="o">*</span><span class="n">window</span><span class="p">;</span>

  <span class="n">gtk_init</span> <span class="p">(</span><span class="o">&amp;</span><span class="n">argc</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">argv</span><span class="p">);</span>

  <span class="n">app</span> <span class="o">=</span> <span class="n">unique_app_new</span> <span class="p">(</span><span class="s">&quot;org.gtk.TestApplication&quot;</span><span class="p">,</span> <span class="nb">NULL</span><span class="p">);</span>

  <span class="k">if</span> <span class="p">(</span><span class="n">unique_app_is_running</span> <span class="p">(</span><span class="n">app</span><span class="p">))</span>
    <span class="p">{</span>
      <span class="n">UniqueResponse</span> <span class="n">response</span><span class="p">;</span>

      <span class="n">response</span> <span class="o">=</span> <span class="n">unique_app_send_message</span> <span class="p">(</span><span class="n">app</span><span class="p">,</span> <span class="n">UNIQUE_ACTIVATE</span><span class="p">,</span> <span class="nb">NULL</span><span class="p">);</span>
      <span class="n">g_object_unref</span> <span class="p">(</span><span class="n">app</span><span class="p">);</span>

      <span class="k">return</span> <span class="n">response</span> <span class="o">==</span> <span class="n">UNIQUE_RESPONSE_OK</span> <span class="o">?</span> <span class="mi">0</span> <span class="o">:</span> <span class="mi">1</span><span class="p">;</span>
    <span class="p">}</span>

  <span class="n">window</span> <span class="o">=</span> <span class="n">create_my_window</span> <span class="p">();</span>

  <span class="n">unique_app_watch_window</span> <span class="p">(</span><span class="n">app</span><span class="p">,</span> <span class="n">GTK_WINDOW</span> <span class="p">(</span><span class="n">window</span><span class="p">));</span>

  <span class="n">gtk_widget_show</span> <span class="p">(</span><span class="n">window</span><span class="p">);</span>

  <span class="n">gtk_main</span> <span class="p">();</span>

  <span class="n">g_object_unref</span> <span class="p">(</span><span class="n">app</span><span class="p">);</span>

  <span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
<span class="p">}</span></pre></td>
      </tr>
    </tbody>
  </table>
</div>

<p>
The same application using GtkApplication:
</p>
<div class="informalexample">
  <table class="listing_frame" border="0" cellpadding="0" cellspacing="0">
    <tbody>
      <tr>
        <td class="listing_lines" align="right"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35</pre></td>
        <td class="listing_code"><pre class="programlisting"><span class="k">static</span> <span class="kt">void</span>
<span class="n">activate</span> <span class="p">(</span><span class="n">GtkApplication</span> <span class="o">*</span><span class="n">app</span><span class="p">)</span>
<span class="p">{</span>
  <span class="n">GList</span> <span class="o">*</span><span class="n">list</span><span class="p">;</span>
  <span class="n">GtkWidget</span> <span class="o">*</span><span class="n">window</span><span class="p">;</span>

  <span class="n">list</span> <span class="o">=</span> <span class="n">gtk_application_get_windows</span> <span class="p">(</span><span class="n">app</span><span class="p">);</span>

  <span class="k">if</span> <span class="p">(</span><span class="n">list</span><span class="p">)</span>
    <span class="p">{</span>
      <span class="n">gtk_window_present</span> <span class="p">(</span><span class="n">GTK_WINDOW</span> <span class="p">(</span><span class="n">list</span><span class="o">-&gt;</span><span class="n">data</span><span class="p">));</span>
    <span class="p">}</span>
  <span class="k">else</span>
    <span class="p">{</span>
      <span class="n">window</span> <span class="o">=</span> <span class="n">create_my_window</span> <span class="p">();</span>
      <span class="n">gtk_window_set_application</span> <span class="p">(</span><span class="n">GTK_WINDOW</span> <span class="p">(</span><span class="n">window</span><span class="p">),</span> <span class="n">app</span><span class="p">);</span>
      <span class="n">gtk_widget_show</span> <span class="p">(</span><span class="n">window</span><span class="p">);</span>
    <span class="p">}</span>
<span class="p">}</span>

<span class="kt">int</span>
<span class="n">main</span> <span class="p">(</span><span class="kt">int</span> <span class="n">argc</span><span class="p">,</span> <span class="kt">char</span> <span class="o">*</span><span class="n">argv</span><span class="p">[])</span>
<span class="p">{</span>
  <span class="n">GtkApplication</span> <span class="o">*</span><span class="n">app</span><span class="p">;</span>
  <span class="n">gint</span> <span class="n">status</span><span class="p">;</span>

  <span class="n">app</span> <span class="o">=</span> <span class="n">gtk_application_new</span> <span class="p">(</span><span class="s">&quot;org.gtk.TestApplication&quot;</span><span class="p">,</span> <span class="mi">0</span><span class="p">);</span>
  <span class="n">g_signal_connect</span> <span class="p">(</span><span class="n">app</span><span class="p">,</span> <span class="s">&quot;activate&quot;</span><span class="p">,</span> <span class="n">G_CALLBACK</span> <span class="p">(</span><span class="n">activate</span><span class="p">),</span> <span class="nb">NULL</span><span class="p">);</span>

  <span class="n">status</span> <span class="o">=</span> <span class="n">g_application_run</span> <span class="p">(</span><span class="n">G_APPLICATION</span> <span class="p">(</span><span class="n">app</span><span class="p">),</span> <span class="n">argc</span><span class="p">,</span> <span class="n">argv</span><span class="p">);</span>

  <span class="n">g_object_unref</span> <span class="p">(</span><span class="n">app</span><span class="p">);</span>

  <span class="k">return</span> <span class="n">status</span><span class="p">;</span>
<span class="p">}</span></pre></td>
      </tr>
    </tbody>
  </table>
</div>

<p>
</p>
</div>
</div>
<br class="example-break"><div class="section">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="id-1.6.5.5"></a>Uniqueness</h2></div></div></div>
<p>
      Instead of creating a UniqueApp with unique_app_new(), create
      a <span class="type">GApplication</span> with <code class="function">g_application_new()</code> or a <a class="link" href="GtkApplication.html" title="GtkApplication"><span class="type">GtkApplication</span></a>
      with <a class="link" href="GtkApplication.html#gtk-application-new" title="gtk_application_new ()"><code class="function">gtk_application_new()</code></a>. The <em class="parameter"><code>name</code></em> that was used with
      <code class="function">unique_app_new()</code> is very likely usable as the <em class="parameter"><code>application_id</code></em> for
      <code class="function">g_application_new()</code> without any changes, and GtkApplication passes
      the <code class="envar">DESKTOP_STARTUP_ID</code> environment variable
      automatically.
    </p>
<p>
      While libunique expects you to check for an already running instance
      yourself and activate it manually, GApplication handles all this on
      its own in <code class="function">g_application_run()</code>. If you still need to find out if there
      is a running instance of your application, use
      <code class="function">g_application_get_is_remote()</code> instead of <code class="function">unique_app_is_running()</code>.
    </p>
</div>
</div>
<div class="footer">
<hr>Generated by GTK-Doc V1.33.1</div>
</body>
</html>